package com.storeroompda.ui.basesync;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.storeroompda.App;
import com.storeroompda.R;
import com.storeroompda.base.views.BaseActivity;
import com.storeroompda.ui.config.ConfigModel;
import com.storeroompda.ui.config.ConfigModelImpl;
import com.storeroompda.ui.config.ConfigPresent;
import com.storeroompda.ui.config.ConfigView;
import com.storeroompda.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BaseSyncActivity extends BaseActivity<BaseSyncModel, BaseSyncView, BaseSyncPresent> implements BaseSyncView {
    private String tag = "BaseSyncActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ivu)
    ImageView ivu;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.ivp)
    ImageView ivp;
    @BindView(R.id.login_p)
    EditText loginP;
    @BindView(R.id.tvr)
    TextView tvr;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.basedown)
    Button basedown;
    @BindView(R.id.baseup)
    Button baseup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basesync);
        ButterKnife.bind(this);
        tvTitle.setText("数据同步");
        loginId.setText(App.sSharedPreferences.getString(Utils.addresskey,""));
        loginP.setText(App.sSharedPreferences.getString(Utils.portkey,""));
//        loginId.setText("10.0.110.32");
//        loginP.setText("8080");
    }

    @Override
    protected void onDestroy() {
        presenter.disconnect();
        super.onDestroy();
    }

    @Override
    public BaseSyncModel createModel() {
        return new BaseSyncModelImpl();
    }

    @Override
    public BaseSyncView createView() {
        return this;
    }

    @Override
    public BaseSyncPresent createPresenter() {
        return new BaseSyncPresent();
    }

    @OnClick({R.id.iv_title_left, R.id.loginbt, R.id.basedown, R.id.baseup})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.loginbt:
                if(loginId.getText().toString().length()==0 || loginP.getText().toString().length()==0)
                {
                    showMsg("请填写地址和端口");
                    return;
                }
                presenter.config(loginId.getText().toString(),loginP.getText().toString());
                break;
            case R.id.basedown:
                tvr.setText("");
                presenter.downinfo();
                break;
            case R.id.baseup:
                tvr.setText("");
                presenter.upinfo();
                break;
        }
    }

    @Override
    public void config(String str) {
        tvr.setText(str);
    }

    @Override
    public void sync(String str) {
        tvr.append(str);
    }

}
